package com.lz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.pojo.SysRole;
import com.lz.service.SysRoleService;
import com.lz.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author tutu
* @description 针对表【t_sys_role(角色信息表)】的数据库操作Service实现
* @createDate 2022-09-15 19:23:19
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Map<String, Object> pageVo(Integer pageNumber, Integer pageSize, SysRole sysRole) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(sysRole.getRolename()),"rolename",sysRole.getRolename());
        IPage<SysRole> page = this.page(new Page<>(pageNumber,pageSize),queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("list",page.getRecords());
        map.put("total",page.getTotal());
        return map;
    }

    @Override
    public SysRole get(Integer id) {
        return sysRoleMapper.selectVoById(id);
    }

    @Override
    public boolean add(SysRole sysRole) {
        this.save(sysRole);
        if (!CollectionUtils.isEmpty(sysRole.getPermissionIdList())){
            //批量新增角色和权限关系
            this.baseMapper.insertRolePermission(sysRole);
        }
        return true;
    }

    @Override
    public boolean edit(SysRole sysRole) {
        //更新角色表信息
        this.updateById(sysRole);
        //删除原有的角色权限关系
        if (sysRole.getPermissionIdList() != null){
            this.baseMapper.deleteRolePermission(sysRole.getId());
            //新增角色权限关系
            if (!sysRole.getPermissionIdList().isEmpty()){
                this.baseMapper.insertRolePermission(sysRole);
            }
        }
        return true;
    }

    @Override
    public List<SysRole> listByUserId(String userId) {
        return this.baseMapper.selectByUserId(userId);
    }


}




